package com.blog.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blog.entity.Product;
import com.blog.myUtils.DateTime;
import com.blog.service.ProductService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mars123
 * @since 2019-07-02
 */
@Service
public class ProductServiceImpl implements ProductService {


    @Override
    public Page<Product> index(Product param, Page<Product> page) {
        EntityWrapper<Product> et = new EntityWrapper<Product>();
        if (param != null && param.getType() != null) {
            et.eq("type", param.getType()).orderBy("created_at", false);
            return new Product().selectPage(page, et);
        }
        return new Product().selectPage(page, new EntityWrapper<Product>().orderBy("created_at", false));
    }

    @Override
    public Product create(Product product) {
        product.setCreatedAt(DateTime.getTime());
        product.setUpdatedAt(product.getCreatedAt());
        if (product.insert()) {
            return product;
        }
        return null;
    }

    @Override
    public Product update(Product product) {
        product.setUpdatedAt(DateTime.getTime());
        if (product.updateById()) {
            return product;
        }
        return null;
    }

    @Override
    public boolean delete(int id) {
        return new Product().deleteById(id);
    }

    @Override
    public Product view(int id) {
        return new Product().selectById(id);
    }
}
